package com.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.dto.KycxAuthorDTO;
import com.domain.entity.KycxAuthorEntity;
import com.domain.vo.KycxAuthorVO;
import com.mapper.KycxAuthorMapper;
import com.mapping.KycxAuthorMapping;
import com.service.IKycxAuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class KycxAuthorServiceImpl extends ServiceImpl<KycxAuthorMapper, KycxAuthorEntity> implements IKycxAuthorService {
    @Autowired
    private KycxAuthorMapping mapping;

    /**
     * 删除作者
     * 根据论文 Kycxid，删除作者信息
     *
     * @param id id
     */
    @Override
    public boolean deleteByKycxId(String id) {

        //根据论文 Kycxid，删除作者信息
        LambdaQueryWrapper<KycxAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KycxAuthorEntity::getKycxId,id);

        return this.baseMapper.delete(queryWrapper)>0;
    }


    /**
     * 根据论文id，查询相关作者信息
     *
     * @param id id
     */
    @Override
    public List<KycxAuthorVO> getInfoByKycxId(String id) {

        LambdaQueryWrapper<KycxAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(KycxAuthorEntity::getKycxId,id);
        queryWrapper.orderByAsc(KycxAuthorEntity::getOrderId);

        List<KycxAuthorEntity> list = this.list(queryWrapper);

        List<KycxAuthorVO> voList = list.stream().map(mapping::entity2Vo).collect(Collectors.toList());
        return voList;
    }

    /**
     * 新增作者
     *
     * @param dto dto
     */
    @Override
    public boolean add(List<KycxAuthorDTO> dto) {

        List<KycxAuthorEntity> entityList = dto.stream().map(mapping::dto2Entity).collect(Collectors.toList());

        if (CollUtil.isNotEmpty(entityList)){
            return this.saveBatch(entityList);
        }

        return true;
    }

    /**
     * 删除作者
     * 根据论文 KycxidList，删除作者信息
     *
     * @param ids List<String>
     */
    @Override
    public boolean deleteByKycxIdList(List<String> ids) {

        if (CollUtil.isEmpty(ids)){
            return true;
        }

        //根据论文 Kycxid，删除作者信息
        LambdaQueryWrapper<KycxAuthorEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(KycxAuthorEntity::getKycxId,ids);

        return this.baseMapper.delete(queryWrapper)>0;

    }
}
